const express = require('express')
const router = express.Router()
const db = require('../../connection/Connection')
const jwt = require('jsonwebtoken')
const SECRET = 'asdfta219*&%^jWJSHFJ'

router.post('/getDepartmentInfo',async (req, res) => {
    console.log(req.body.token)
    const result = jwt.verify(req.body.token, SECRET)
    let sql = 'select * from user where username = ?'
    db.base(sql, result.username, response => {
        //验证成功
        console.log(response)
        if(response.length > 0) {
            //判断是否存在region，存在则根据region返回数据
            // console.log(req.body.region)
            //返回全部分数据
            if(req.body.region === null || req.body.region === 'non_choice') {
                let sql = 'select * from departmentInfo_nostation'
                db.base(sql, null, res1 => {
                    //统计部门ID
                    var n = []
                    let data = []
                    for (let i = 0; i < res1.length; i++) {
                        n.push(res1[i].department_id)
                        data[i] = {
                            'stations': '',
                            'departmentName': res1[i].department_name,
                            'region':  res1[i].region,
                            'departmentHeadName':  res1[i].department_head,
                            'departmentFunction':  res1[i].department_function,
                            'department_id':  res1[i].department_id
                        }
                    }


                    console.log('n=', n)
                    // console.log('data=', data)

                    //地区选项数据
                    let options  = []
                    let m =[]
                    for (let i = 0; i < res1.length; i ++ ){
                        if (m.indexOf(res1[i].region) === -1)
                            m.push(res1[i].region)
                    }
                    for (let j = 0; j < m.length; j++){
                        options[j] = {
                            'value': m[j],
                            'label': m[j]
                        }
                    }
                    options[m.length] = {
                        'value': 'non_choice',
                        'label': '未选择'
                    }
                    // console.log('options=',options)
                    let sql = 'select * from departmentInfo'
                    db.base(sql, null, response => {

                        for (let i = 0; i < n.length; i++) {
                            let station = ''
                            for (let j = 0; j < response.length; j++) {
                                if (response[j].department_id === n[i]){
                                    station = station + '/' +response[j].station_name
                                }
                            }
                            // console.log(station)
                            data[i].stations = station
                        }
                        // console.log('data=', data)
                        res.json({
                            'data': data,
                            'options': options
                        })
                    })
                })
            }
            //   返回region指定数据
            else {
                //查询指定地区的部门
                let sql = 'select * from departmentInfo_nostation where region = ?'
                db.base(sql, req.body.region, res2 => {
                    let n =[]
                    let data = []
                    for (let i = 0; i < res2.length; i ++ ){
                        n.push(res2[i].department_id)
                        data[i] = {
                            'stations': '',
                            'departmentName': res2[i].department_name,
                            'region':  res2[i].region,
                            'departmentHeadName':  res2[i].department_head,
                            'departmentFunction':  res2[i].department_function,
                            'department_id':  res2[i].department_id
                        }
                    }
                    // console.log('n=',n)
                    // console.log('data=',data)
                    let sql = 'select * from departmentInfo where region = ?'
                    db.base(sql, req.body.region, response => {
                        for (let i = 0; i < n.length; i++) {
                            let station = ''
                            for (let j = 0; j < response.length; j++) {
                                if (response[j].department_id === n[i]){
                                    station = station + '/' +response[j].station_name
                                }
                            }
                            // console.log(station)
                            data[i].stations = station
                        }
                        // console.log('data=',data)
                        res.json({data})
                    })
                })
            }
        }
    })

})

module.exports = router
